package com.hzf.material.admin.controller.manager;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.hzf.api.rpc.api.ApiService;
import com.hzf.common.base.BaseController;
import com.hzf.common.validator.LengthValidator;
import com.hzf.config.dao.model.ConfigRegion;
import com.hzf.config.dao.model.ConfigRegionExample;
import com.hzf.material.dao.model.*;
import com.hzf.material.rpc.api.MaterialListService;
import com.hzf.material.rpc.api.MaterialListTypeService;
import com.hzf.upms.common.constant.UpmsResult;
import com.hzf.upms.common.constant.UpmsResultConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * describe: 商场查询
 * creat_user: 白开水
 * creat_time: 2017/11/16   18:26
 **/
@Controller
@Api(value = "材料商商场类型管理", description = "材料商管理")
@RequestMapping("/manage/marketList")
public class MaterialListController extends BaseController {
    private static Logger _log = LoggerFactory.getLogger(MaterialListTypeController.class);

    @Autowired
    private MaterialListService materialListService;

    @Autowired
    private ApiService apiService;

    @Autowired
    private MaterialListTypeService materialListTypeService;


    @ApiOperation(value = "商场列表首页")
    @RequiresPermissions("material:marketlist:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/materialList/index.jsp";

    }

    @ApiOperation(value="基础商场列表")
    @RequiresPermissions("material:marketlist:read")
    @RequestMapping(value="/list",method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        System.out.println("<<<<jinlaile>>>>>");
        MaterialListExample materialListExample = new MaterialListExample();
        if(!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)){
            materialListExample.setOrderByClause(sort+" "+order);
        }
        List<MaterialList> rows = materialListService.selectByExampleForOffsetPage(materialListExample,offset,limit);
        long total = materialListService.countByExample(materialListExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增商场")
    @RequiresPermissions("material:marketlist:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Model model) {
        ConfigRegionExample  configRegionExample = new ConfigRegionExample();
        configRegionExample.createCriteria().andRegionTypeEqualTo(new Long((long)1));
        List<ConfigRegion> configRegions = apiService.selectRegion(configRegionExample);
        model.addAttribute("rows",configRegions);
        MaterialListTypeExample materialListTypeExample = new MaterialListTypeExample();
        List<MaterialListType> list =materialListTypeService.selectByExample(materialListTypeExample);
        model.addAttribute("materIallistType",list);
        return "/manage/materialList/create.jsp";
    }

    @ApiOperation(value = "新增商场")
    @RequiresPermissions("material:marketlist:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(MaterialList materialList){
        ComplexResult result = FluentValidator.checkAll()
                .on(materialList.getMarketListName(), new LengthValidator(1, 50, "商场名称"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
        }
        int count = materialListService.insertSelective(materialList);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除商场")
        @RequiresPermissions("material:marketlist:delete")
        @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
        @ResponseBody
        public Object delete(@PathVariable("ids") String ids) {

        System.out.println("<jinlaile>///////"+ids);
            int count = materialListService.deleteByPrimaryKeys(ids);
            return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }


    @ApiOperation(value = "修改类型")
    @RequiresPermissions("material:marketlist:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        MaterialList materialList = materialListService.selectByPrimaryKey(id);
        modelMap.put("materialList", materialList);
        return "/manage/materialList/update.jsp";
    }

    @ApiOperation(value = "修改类型")
    @RequiresPermissions("material:markettype:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") int id, MaterialList materialList) {
        ComplexResult result = FluentValidator.checkAll()
                .on(materialList.getMarketListName(), new LengthValidator(1, 50, "商场名称"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
        }
        materialList.setMarketListId(id);
        int count = materialListService.updateByPrimaryKeySelective(materialList);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

}
